package com.itheima.controller;

import cn.hutool.core.util.RandomUtil;


import com.alibaba.fastjson.JSON;
import com.itheima.autoconfig.template.SmsTemplate;
import com.itheima.common.R;
import com.itheima.constant.TokenConstant;
import com.itheima.dto.UserDto;
import com.itheima.pojo.User;
import com.itheima.pojo.UserInfo;
import com.itheima.service.UserService;
import com.itheima.vo.UserVo;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author CSY
 * 2022/5/18    15:05
 */
@RestController
@RequestMapping("/user")
@Slf4j
@ConfigurationProperties(prefix = "th.oss")
public class UserController {

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private SmsTemplate smsTemplate;

    @Resource
    private UserService userService;

    /**
     * 获取手机号，进行验证码的获取
     * @param user
     */
    @PostMapping("/login")
    public void login(@RequestBody User user){

        if (user.getPhone()!=null){
            int code = RandomUtil.randomInt(100000, 999999);

            smsTemplate.sendSms(user.getPhone(),String.valueOf(code));

            redisTemplate.opsForValue().set(user.getPhone(),String.valueOf(code));
        }


    }

    /**
     * 与是否为新用户
     * 返回token令牌
     * @param userDto
     * @return
     */
    @PostMapping("/loginVerification")
    public R<UserVo> loginV(@RequestBody UserDto userDto) throws Exception {

        //获取手机号和验证码
        String phone = userDto.getPhone();
        String verification = userDto.getVerificationCode();
        //获取redis中保存的验证码
        String code = (String) redisTemplate.opsForValue().get(phone);
        assert code != null;
        if (code.equals(verification)){
            //当相等时，就去查询数据库是否有相同手机号的用户，有放行，没有创建后放行
            UserDto userDto1 = userService.getUser(phone);
            boolean b1 = userDto1 == null;
            if (b1){
                boolean b = userService.addUser(userDto);
            }
            String tokenString = System.currentTimeMillis() + phone;
            String token = DigestUtils.md5DigestAsHex(tokenString.getBytes());
            redisTemplate.opsForValue().set(TokenConstant.USER_TOKEN_PREFIX+token, JSON.toJSONString(userDto1),48L, TimeUnit.HOURS);

            UserVo userVo = new UserVo();
            userVo.setIsNew(b1);
            userVo.setToken(token);

            return R.success(userVo);


        }


        return R.error("1");
    }

    @PostMapping("/loginReginfo")
    public R regInfo(@RequestBody UserInfo userInfo){

        return R.success("11111111111111");
    }




}
